﻿using DetectScars.model;
using MathNet.Numerics.Providers.SparseSolver;
using ScottPlot;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZstdNet;

namespace DetectScars.common
{
    /** C扫过程中的A扫数据处理工具类
     * 
     */
    class AcqData
    {
        //通道
        public static List<ushort> channels = new List<ushort>();
        //A扫
        public static DataAandC[] dataAs;
        //端面c扫
        public static double[,] dataC1;
        //侧面c扫
        public static double[,] dataC2;
        //端面c扫A扫
        public static double[,][] dataCA1;

        //扫描记录开始标记
        public static bool start;
        //上次光电标记
        private static double last_gdbj;
        //光电标记
        private static double gdbj;
        //光电标记次数
        private static int gdbj_count;
        //端面距离
        public static double dm_s;
        //端面速度
        public static double dm_v;
        //端面运动状态
        public static bool dm_status;
        //侧面距离
        //侧面速度

        static int ind_x = 0;
        static int ind_y = 0;
        public static void Init()
        {
            dataAs = new DataAandC[12];
            dataC1 = new double[160, 6965];
            dataCA1 = new double[160, 6965][];
            start = false;
            ind_x = 0;
            ind_y = 0;
            gdbj_count = 0;
            gdbj = 0;
            last_gdbj = 0;
            dm_status = false;
            SocttPlotUtils.dataC2 = null;
            SocttPlotUtils.dataC1 = null;
        }
        public static void Clear()
        {

        }

        public static void AddDataA(int index, DataAandC dataa)
        {
            if (!start)
            {
                return;
            }
            if (index != 0)
            {
                return;
            }
            if (ind_x==0|| ind_y==0)
            {
                return;
            }
            int x = dataa.U1134_Encode1;
            int y = dataa.U1130_Encode0;
            x = ind_x - x;
            y = y - ind_y;
            x = x / 200;
            y = y / 10;
            if (x >= dataC1.GetLength(0) || x < 0)
            {
                return;
            }
            if (y >= dataC1.GetLength(1) || y < 0)
            {
                return;
            }
            
            int index_ = x + 10;
            if (x + 10 >= dataC1.GetLength(0))
            {
                index_ = dataC1.GetLength(0);
            }
            for (int i = x; i < index_; i++)
            {
                dataCA1[i, y] = dataa.U10_DataA;
            }
        }

        public static void AddDataC1(int index,double[][] datacs)
        {
            if (!start)
            {
                return;
            }
            if (index!=0)
            {
                return;
            }
            //LogUtils.WriteLog("=====1=====");
            foreach (double[] datac in datacs)
            {
                gdbj = datac[2];
                //c轴
                int y = SocketUtils.byte2Int32((byte)datac[4], (byte)datac[5], (byte)datac[6], (byte)datac[7]);
                //LogUtils.WriteLog("=="+y+"==");
                int wc = Math.Abs(y - ind_y);
                if (last_gdbj == 0 && gdbj == 1 && wc>1000)
                {
                    LogUtils.WriteLog("光电标记：" + y);
                    gdbj_count += 1;
                    
                    ind_y = y;
                }
                //LogUtils.WriteLog("光电：" + gdbj);
                last_gdbj = gdbj;
                if (gdbj_count == 0)
                {
                    continue;
                }
                //x轴
                int x = SocketUtils.byte2Int32((byte)datac[8], (byte)datac[9], (byte)datac[10], (byte)datac[11]);
                if (ind_x == 0)
                {
                    ind_x = x;
                }
                if (gdbj_count == 2)
                {
                    //x轴运动到指定位置
                    if (!dm_status)
                    {
                        dm_status = true;
                        PlcControlUtils.startRelativeRun("X", false);
                        PlcControlUtils.setRelativeSpeed("X", dm_v);
                        PlcControlUtils.setRelativePosition("X", dm_s);
                        PlcControlUtils.startRelativeRun("X", true);
                    }
                    
                }
                if (gdbj_count >= 9)
                {
                    start = false;
                    //停止旋转
                    PlcControlUtils.cStop();
                }
                x = ind_x - x;
                y = y - ind_y;
                x = x / 200;
                y = y / 10;
                if (x>= dataC1.GetLength(0)|| x<0)
                {
                    continue;
                }
                if (y>= dataC1.GetLength(1) || y<0)
                {
                    continue;
                }
                int index_ = x + 10;
                if (x+10>= dataC1.GetLength(0))
                {
                    index_ = dataC1.GetLength(0);
                }
                for(int i=x;i< index_; i++)
                {
                    dataC1[i, y] = SocketUtils.byte2Int16((byte)datac[26], (byte)datac[27]);
                }

                //LogUtils.WriteLog(x + "==" + y);
            }

        }

    }
}
